package com.hxh.ocean.controller
{
	import com.hxh.ocean.helpers.*;
	
	import flash.events.Event;
	import flash.media.*;
	import flash.net.URLRequest;

	public class MusicManager {

		// Private Properties:
		private var _bgChannel:SoundChannel;
		private var _bgMusic:Sound;

		// Initialization:
		public function MusicManager(play:Boolean) {
			_bgMusic=new Sound(new URLRequest(Helper.SOUND_BACKGROUND));
			playMusic();
			if (!play){
				_bgChannel.stop();	
			}
		}

		public function toggleMusic(play:Boolean):void {
			if (play) {
				playMusic();
			} else {
				_bgChannel.stop();
			}
		}
		
		public function playMusic():void{
			_bgChannel=_bgMusic.play();
			_bgChannel.soundTransform=new SoundTransform(0.5);
			_bgChannel.addEventListener(Event.SOUND_COMPLETE, loopMusic);
		}

		private function loopMusic(evt:Event):void {
			_bgChannel.removeEventListener(Event.SOUND_COMPLETE, loopMusic);
			playMusic();
		}
	}
}